<?php
set_time_limit(0);
include '../../common.inc.php';
timer_start();
$dbSource = new PDO('mysql:host=192.168.1.19;dbname=caiji', 'root', 'xddlxdcw');
$rootFolder = "//192.168.1.89/autopic";
$cacheFile = 'autoPictureHandler.php';
if (file_exists(DT_CACHE.'/'.$cacheFile)) {
	$cacheContent = PCache::read($cacheFile);
} else {
	$subDir =  File::subDir($rootFolder);
	$subDirUtf8 = __($subDir)->map(function($item){
		return Str::convert($item, 'gbk', 'utf-8');
	});
	PCache::write($cacheFile,$subDirUtf8);
	$cacheContent = PCache::read($cacheFile);
}
if (empty($cacheContent)) {
	exit('EMPTY Cache file ' . $cacheFile);
}

if ($index) {
	$index = intval($index);
} else {
	$index = 0;
}

if ($cacheContent[$index]) {
	$dirs = File::listDir(Str::convert($cacheContent[$index], 'utf-8', 'gbk'));
} else {
	exit('遍历目录OVER');
}

$dirsUtf8 =  __($dirs)->map(function($item){
	return Str::convert($item, 'gbk', 'utf-8');
});

$insert = "insert into picfolders (name) values";
foreach($dirsUtf8 as $aim) {
	$insert .= "('".str_replace($rootFolder, '', $aim)."'),";
}
$insert = rtrim($insert, ',');
$dbSource->query($insert);
File::append(__DIR__.'/time', timer_end()."\t".count($dirsUtf8));
$index +=1;
echo '<meta http-equiv="refresh" content="1; url='.$_SERVER['PHP_SELF'].'?index='.$index.'" />';




